<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head >
   <title>[[${title}]]</title>
   <link rel="stylesheet" type="text/css" href="/css/hy.css">
    <script type="text/javascript" src="/js/index.js"></script>
    <script type="text/javascript" src="/js/hy.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<body >

<div class="layui-body" style="padding:15px;top: 0px;">
    <div class="layui-inline">
        <form class="layui-form layui-form-pane1" method="get" >
            <div class="layui-form-item">
                <div class="layui-inline"> 
                    <input type="text" name="amount" id="amount"  placeholder="支付金额" value="100" class="layui-input" style="width: 200px" />
                </div>
                <div class="layui-inline">
                    <input type="text" name="receive_name" id="receive_name"  placeholder="银⾏账户名" class="layui-input" style="width: 200px" />
                </div>
                <div class="layui-inline">
                    <input type="text" name="receive_account" id="receive_account"  placeholder="⽤户银⾏账号或 UPI 账号" class="layui-input" style="width: 200px" />
                </div>
            </div>
            <div class="layui-form-item">
                <button type="button" id="pay" class="layui-btn">支付</button>
            </div>
            

        </form>
    </div>
</div>
<script src="https://cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>

<script>
    $(function(){
        $("#pay").click(function(){
            var data = {
                "merchant_id":'12002',
                "order_id":'',
                "amount":0.01,
                "transfer_type":2,
                "receive_name":$("#receive_name").val(),
                "receive_account":$("#receive_account").val(),
                "ifsc_code":"",
                "notify_url":""//"http://localhost:8080/pay_result"
            }
            let date = new Date()
            let orderId =dateFormat("YYYYmmddHHMM", date)+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);

            data['order_id']=orderId;
            data['amount']=$("#amount").val();

            console.log(data);
            
            let s_data = sort_ascii(data);
            console.log(s_data);
            let sign = md5(s_data).toString();
            console.log(sign);
            data['sign']=sign;
            var url = "https://pay.3kcpay.com/api/india/withdrawal";
            $.ajax({
                    url: url,
                    type: "POST",
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (res) {
                        console.log(res);
                    }
                });
      
            return false;
        })

        /**按ascii码从小到大排序
         *
         * @param obj
         * @returns {string}
         */
         function sort_ascii(obj) {
            let arr = new Array();
            let num = 0;
            for (let i in obj) {
                arr[num] = i;
                num++;
            }
            let sortArr = arr.sort();
            //let sortObj = {};    //完成排序值
            let str = '';             //自定义排序字符串
            for (let i in sortArr) {
                str += sortArr[i] + '=' + obj[sortArr[i]] + '&';
                //sortObj[sortArr[i]] = obj[sortArr[i]];
            }
            //去除两侧字符串
            let char = '&'
            str = str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
 
            return str;
            //return sortObj;
        }

        function dateFormat(fmt, date) {
            let ret;
            const opt = {
                "Y+": date.getFullYear().toString(),        // 年
                "m+": (date.getMonth() + 1).toString(),     // 月
                "d+": date.getDate().toString(),            // 日
                "H+": date.getHours().toString(),           // 时
                "M+": date.getMinutes().toString(),         // 分
                "S+": date.getSeconds().toString()          // 秒
                // 有其他格式化字符需求可以继续添加，必须转化成字符串
            };
            for (let k in opt) {
                ret = new RegExp("(" + k + ")").exec(fmt);
                if (ret) {
                    fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
                };
            };
            return fmt;
        }
 
       

    })
</script>
</body>
</html>